import { createRouter, createWebHistory } from 'vue-router'
import { checkRoutePermission } from '@/utils/auth'
import { ElMessage } from 'element-plus'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'root',
            redirect: '/login'
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/Login.vue')
        },
        {
            path: '/home',
            name: 'home',
            component: () => import('../views/Home/index.vue'),
            meta: {
                requiresAuth: true
            },
            children: [
                {
                    path: '',
                    redirect: '/home/info'
                },
                {
                    path: 'info',
                    name: '信息',
                    component: () => import('../views/Home/Info.vue')
                },
                {
                    path: 'shop',
                    name: '商店',
                    component: () => import('../views/Home/Shop.vue')
                },
                {
                    path: 'loans',
                    name: '融资',
                    component: () => import('../views/Home/Loans.vue')
                },
                {
                    path: 'space',
                    name: '个人空间',
                    component: () => import('../views/Home/Space.vue'),
                    meta: {
                        requiresAuth: true
                    }
                },
                {
                    path: 'userControl',
                    name: '用户管理',
                    component: () => import('../views/Home/UserControl.vue'),
                    meta: {
                        requiresAuth: true,
                        requiresAdmin: true
                    }
                },
                {
                    path: 'loansControl',
                    name: '贷款管理',
                    component: () => import('../views/Home/LoansControl.vue'),
                    meta: {
                        requiresAuth: true,
                        requiresAdmin: true
                    }
                }
            ]
        },
        {
            path: '/:pathMatch(.*)*',
            name: '404',
            component: () => import('../views/NotFound.vue')
        }
    ]
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
    console.log('路由从', from.fullPath, '跳转到了：', to.fullPath)

    if(to.fullPath !== '/home/info') {
        // 检查路由权限
        if (!checkRoutePermission(to)) {

            // 如果需要管理员权限但用户不是管理员
            if (!to.meta?.requiresAdmin) {
                ElMessage.error('您没有权限访问该页面')
                next('/home')
                return
            }

            // 如果需要登录但用户未登录
            if (to.meta?.requiresAuth && localStorage.getItem('token') === null) {
                ElMessage.warning('请先登录')
                next('/login')
                return
            }
        }
    }
    next()
})

export default router
